package com.bestdata.em.datasyn.northinterface;

import com.bestdata.em.datasyn.datareceive.model.RawData;
import com.bestdata.em.datasyn.queue.MsgQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 提供数据库同步接口
 * return 测值、时间
 * @author chizhenyu
 */
@RestController
@RequestMapping("/api/")
public class DataSynAPI {

    private Logger logger = LoggerFactory.getLogger(DataSynAPI.class);

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 监听LS表的insert状况
     */
    @RequestMapping("dataSyn_LSData")
    public @ResponseBody void dataSyn_LSData(String stationName, String time, String X, String Y, String H){

        //todo sqlserver触发器-监听insert，成功触发后返回插入数据ID(也可能无参数，具体情况待成功配置触发器)

        //todo 原则上应该新建三个接口，对应三个不同的监测点，加入队列后统一转交到drs

        //todo 华测监测点需要和佰达做对应

        RawData rawData = new RawData();
        Date parse = new Date();

        try {
            parse = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        rawData.init(stationName,parse.getTime(),X,Y,H);

        try {
            MsgQueue.Queue.put(rawData);
        } catch (InterruptedException e) {
            logger.error("dataSyn_LSData is not put queue!!!Queue.size() = {}",MsgQueue.Queue.size());
            e.printStackTrace();
        }

    }

    /**
     * 监听XK表的insert状况
     */
    @RequestMapping("dataSyn_XKData")
    public @ResponseBody void dataSyn_XKData(String stationName, String time, String X, String Y, String H){

        RawData rawData = new RawData();
        Date parse = new Date();

        try {
            parse = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        rawData.init(stationName,parse.getTime(),X,Y,H);

        try {
            MsgQueue.Queue.put(rawData);
        } catch (InterruptedException e) {
            logger.error("dataSyn_XKData is not put queue!!!Queue.size() = {}",MsgQueue.Queue.size());
            e.printStackTrace();
        }

    }

    /**
     * 监听PH表的insert状况
     */
    @RequestMapping("dataSyn_PHData")
    public @ResponseBody void dataSyn_PHData(String stationName, String time, String X, String Y, String H){

        RawData rawData = new RawData();
        Date parse = new Date();

        try {
            parse = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        rawData.init(stationName,parse.getTime(),X,Y,H);

        try {
            MsgQueue.Queue.put(rawData);
        } catch (InterruptedException e) {
            logger.error("dataSyn_PHData is not put queue!!!Queue.size() = {}",MsgQueue.Queue.size());
            e.printStackTrace();
        }

    }

    /**
     * 监听整编计算好得GNSS  XYZ  结果值
     * 【【【暂时只使用此接口用以数据同步】】】
     */
    @RequestMapping("dataSyn_resultData")
    public @ResponseBody void dataSyn_resultData(String stationName, String time, String X, String Y, String H){

        RawData rawData = new RawData();
        Date parse = new Date();

        try {
            parse = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        rawData.init(stationName,parse.getTime(),X,Y,H);

        try {
            MsgQueue.Queue.put(rawData);
        } catch (InterruptedException e) {
            logger.error("dataSyn_resultData is not put queue!!!Queue.size() = {}",MsgQueue.Queue.size());
            e.printStackTrace();
        }

    }


    @RequestMapping("dataSyn_test")
    public @ResponseBody void dataSyn_test(String stationName, String time, String X, String Y, String H){

        RawData rawData = new RawData();
        Date parse = new Date();

        try {
            parse = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        rawData.init(stationName,parse.getTime(),X,Y,H);

        System.out.println("++++++++++++++++++++++++");
        System.out.println(rawData);
    }

}
